package com.chatmusicstatus.service;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

public class XMPPStatusSetter {
    private XMPPConnection connection=null;
    
    public XMPPStatusSetter(String server, int port, String service, String uname, String Pass) throws XMPPException
    {
        ConnectionConfiguration conf;
        if("".equals(service))
             conf = new ConnectionConfiguration(server, port);
        else
            conf = new ConnectionConfiguration(server, port, service);
        connection = new XMPPConnection(conf);
        connection.connect();
        connection.login(uname, Pass);
    }
    
    public void Close() {
        connection.disconnect();
        connection=null;
    }
    
    public boolean setStatus(String t) {
        if(connection==null || connection.isAuthenticated()==false)
            return false;
        Presence presence = new Presence(Presence.Type.available);
        presence.setStatus(t);
        presence.setPriority(24);
        presence.setMode(Presence.Mode.available);
        connection.sendPacket(presence);
        return true;
    }
}
